package com.otod.sys.role.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.otod.base.exception.AppException;
import com.otod.base.service.BaseService;
import com.otod.commons.entity.Page;
import com.otod.commons.utils.StringUtils;
import com.otod.sys.emp.mapper.EmpOrgRoleMapper;
import com.otod.sys.role.mapper.RoleMapper;
import com.otod.sys.role.mapper.RoleMenuMapper;
import com.otod.sys.role.service.IRoleService;
import com.otod.sys.vo.AuapRRoleMenuVo;
import com.otod.sys.vo.AuapTRoleVo;

/**
 * @author jqiu
 * update by superzoc 2014/05/15 增加保存角色菜单的事务处理
 */
@Service
@Transactional(readOnly=true)
public class RoleServiceImpl extends BaseService implements IRoleService {
    @Autowired
    RoleMapper roleMapper;

    @Autowired
    RoleMenuMapper roleMenuMapper;
    @Autowired
    EmpOrgRoleMapper empOrgRoleMapper;

    /**
     * 获取角色VO
     *
     * @param vo
     */
    public AuapTRoleVo getById(String id) {
        return roleMapper.getById(id);
    }


    /**
     * 分页查询员工信息列表
     *
     * @param page  分页信息
     * @param param 参数
     * @return 分页信息和结果数据
     */
    public Page queryForList(Page page, Map<String, Object> param) {
        List<AuapTRoleVo> dataList = roleMapper.queryForList(page, param);
        page.setAaData(dataList);
        return page;
    }

    /**
     * 查询员工信息列表
     *
     * @param param 参数
     * @return 分页信息和结果数据
     */
    public List<AuapTRoleVo> queryForList(Map<String, Object> param) {
        List<AuapTRoleVo> dataList = roleMapper.queryForList(param);
        return dataList;
    }

    /**
     * 更新角色
     */
    @Override
    @Transactional
    public void updateRole(Map map) {
        roleMapper.update(map);
    }

    /**
     * 新增角色
     */
    @Override
    @Transactional
    public void addRole(AuapTRoleVo vo) {
        vo.setUuid(super.getUUID());
        roleMapper.insert(vo);
    }


    /**
     * 删除角色
     * @throws AppException 
     */
    @Override
    @Transactional
    public void delRole(String roleId) throws AppException {
    	
    	AuapTRoleVo vo = this.getById(roleId);
    	if("admin".equals(vo.getRoleNo())){
    		throw new AppException("管理员角色不能删除！");
    	}
    	
    	/*删除机构角色菜单关系*/
    	this.delRoleMenuId(roleId);
    	/*删除员工机构角色关系*/
    	empOrgRoleMapper.deleteByRoleId(roleId);
        //删除角色
        roleMapper.deleteById(roleId);



    }

    /**
     * 查询全部角色
     *
     * @param id
     */
    public List<AuapTRoleVo> findAll() {
        return roleMapper.queryForList(new HashMap<String, Object>());
    }


    @Override
    public List<AuapRRoleMenuVo> findAllMenu(String roleId) {

        return roleMenuMapper.findByRoleId(roleId);
    }


    @Override
    @Transactional
    public void saveRoleMenuId(String roleId, String menuIds) {
        this.delRoleMenuId(roleId);
        String[] str = StringUtils.split(menuIds, ",");
        for (String menuId : str) {
            AuapRRoleMenuVo vo = new AuapRRoleMenuVo();
            vo.setRoleId(roleId);
            vo.setMenuId(menuId);
            vo.setUuid(super.getUUID());
            roleMenuMapper.insert(vo);
        }
    }


    @Override
    @Transactional
    public void delRoleMenuId(String roleId) {
        roleMenuMapper.deleteByRoleId(roleId);

    }




}
